/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.ipti.br.tag.bean;

import org.ipti.br.tag.bean.lazy.LazyUnityDataModel;
import org.ipti.br.tag.bo.ActorBO;
import org.ipti.br.tag.bo.AdditionalInfoBO;
import org.ipti.br.tag.bo.JuridicBO;
import org.ipti.br.tag.bo.OrganizationBO;
import org.ipti.br.tag.bo.UnityBO;
import org.ipti.br.tag.entity.Actor;
import org.ipti.br.tag.entity.AdditionalInfo;
import org.ipti.br.tag.entity.Juridic;
import org.ipti.br.tag.entity.Unity;
import java.util.List;
import javax.enterprise.context.SessionScoped;
import javax.inject.Inject;
import javax.inject.Named;

/**
 *
 * @author israel
 */
@Named(value = "unitySearch")
@SessionScoped
public class UnitySearchBean extends AppBaseBean {

    @Inject
    private ActorBO actorBO;
    @Inject
    private AdditionalInfoBO additionalInfoBO;
    @Inject
    private JuridicBO juridicBO;
    @Inject
    OrganizationBO organizationBO;
    @Inject
    private UnityBO unityBO;
    @Inject
    private SessionBean sessionBean;
    private Unity unity;
    private Long organizationID;
    private int organizationType;
    private LazyUnityDataModel unities;

    public UnitySearchBean() {
    }

    public String edit() {
        return "/educational/edit/unityEdit.xhtml?unity=" + unity.getId() + "&type=" + organizationType + "&faces-redirect=true";
    }

    public String getOrganizationName() {
        return getOrganizationBO().find(organizationID).getName();
    }

    public void init() {
        unities = new LazyUnityDataModel(unityBO.getUnityDAO(), unityBO.unityChildrenPerOrganization2(sessionBean.getLoggedActor().getUnity().getId(),
                organizationID));
    }

    public void delete() {
        try {
            if (true) {
                getUnityBO().remove(unity);
                addInfoMessage("unitySearchForm", "Exclusão realizada com sucesso", null);
            }else{
                addErrorMessage("unitySearchForm", "Impossível deletar unidade que tenham dependências.", null);
            }
        } catch (Exception ex) {
            addErrorMessage("unitySearchForm", "Um erro inesperado foi encontrado", null);
        }
        init();
    }
    int totalMatriculatedChildren = 0;
    
    public boolean dependecyCheck(Unity unityT){
        if(unityT.getActors().isEmpty()){
            if(getUnityBO().getUnityChildren(unityT.getId()).isEmpty()){
                return true;
            }
        }
        return false;
    }

    public int loadTotalMatriculatedChildren(Unity unity) {
        return unityBO.getStudentsBySchool(unity).size();

    }
    
    /*
     * Getters and Setters
     */

    public ActorBO getActorBO() {
        return actorBO;
    }

    public void setActorBO(ActorBO actorBO) {
        this.actorBO = actorBO;
    }

    public AdditionalInfoBO getAdditionalInfoBO() {
        return additionalInfoBO;
    }

    public void setAdditionalInfoBO(AdditionalInfoBO additionalInfoBO) {
        this.additionalInfoBO = additionalInfoBO;
    }

    public JuridicBO getJuridicBO() {
        return juridicBO;
    }

    public void setJuridicBO(JuridicBO juridicBO) {
        this.juridicBO = juridicBO;
    }

    public OrganizationBO getOrganizationBO() {
        return organizationBO;
    }

    public void setOrganizationBO(OrganizationBO organizationBO) {
        this.organizationBO = organizationBO;
    }

    public UnityBO getUnityBO() {
        return unityBO;
    }

    public void setUnityBO(UnityBO unityBO) {
        this.unityBO = unityBO;
    }

    public SessionBean getSessionBean() {
        return sessionBean;
    }

    public void setSessionBean(SessionBean sessionBean) {
        this.sessionBean = sessionBean;
    }

    public Unity getUnity() {
        return unity;
    }

    public void setUnity(Unity unity) {
        this.unity = unity;
    }

    public Long getOrganizationID() {
        return organizationID;
    }

    public void setOrganizationID(Long organizationID) {
        this.organizationID = organizationID;
    }

    public int getOrganizationType() {
        return organizationType;
    }

    public void setOrganizationType(int organizationType) {
        this.organizationType = organizationType;
    }

    public LazyUnityDataModel getUnities() {
        return unities;
    }

    public void setUnities(LazyUnityDataModel unities) {
        this.unities = unities;
    }

    public int getTotalMatriculatedChildren() {
        return totalMatriculatedChildren;
    }

    public void setTotalMatriculatedChildren(int totalMatriculatedChildren) {
        this.totalMatriculatedChildren = totalMatriculatedChildren;
    }
}